<?php
class PerformanceMetrics extends HTech_Db_Table
{
    protected $_name    = 'performance_metrics';
    protected $_primary = 'PerformanceMetricId'; 
    
    private $_subject;
    private $_customMarks = array();
    
    public function __construct($subject, $config = array()) {
        parent::__construct($config);
        $this->_subject = $subject;
    }

    protected function _setGridMetadata()
    {
        $this->_gridMetadata['title'] = $this->translate("Performance metrics");
        $this->_gridMetadata['titleMods'] = array('Subject');
        $this->_gridMetadata['onClick'] = 'admin.performance-metric.add';
        $this->_gridMetadata['params'] = array('Subject');
        $this->_gridMetadata['headers'] = array
            (
                array('PerformanceMetricId', 50, $this->translate("Id"),           'Number'),
                array('Subject',             85, $this->translate("Subject"),      'String'),
                array('Metric',             150, $this->translate("Metric"),       'String'),
                array('A',                  100, $this->translate("Mark A"),       'Number',   'Standard'),
                array('B',                  100, $this->translate("Mark B"),       'Number',   'Standard'),
                array('C',                  100, $this->translate("Mark C"),       'Number',   'Standard'),
                array('ScoreAA',            120, $this->translate("Score A -"),    'Number',   'Standard'),
                array('ScoreAB',            120, $this->translate("Score A to B"), 'Number',   'Standard'),
                array('ScoreBC',            120, $this->translate("Score B to C"), 'Number',   'Standard'),
                array('ScoreCC',            120, $this->translate("Score C +"),    'Number',   'Standard'),
                array('ColorAA',            120, $this->translate("Color A -"),    'String'),
                array('ColorAB',            120, $this->translate("Color A to B"), 'String'),
                array('ColorBC',            120, $this->translate("Color B to C"), 'String'),
                array('ColorCC',            120, $this->translate("Color C +"),    'String')
            );
        return $this;
    }
    
    public function getScore($val,$metric)
    {
        $select = $this->select();         
        $select->where(sprintf("Subject = '%s'", $this->_subject));
        $select->where(sprintf("Metric = '%s'", $metric));
        $opt = current($this->fetchAll($select)->toArray());
        
        $a = isset($this->_customMarks[$metric]['A']) ? $this->_customMarks[$metric]['A'] : $opt['A'];
        $b = isset($this->_customMarks[$metric]['B']) ? $this->_customMarks[$metric]['B'] : $opt['B'];
        $c = isset($this->_customMarks[$metric]['C']) ? $this->_customMarks[$metric]['C'] : $opt['C'];
        
        if ($val < $a) return array('score' => $opt['ScoreAA'], 'color' => $opt['ColorAA']);
        if ($val < $b) return array('score' => $opt['ScoreAB'], 'color' => $opt['ColorAB']);
        if ($val < $c) return array('score' => $opt['ScoreBC'], 'color' => $opt['ColorBC']);
        return array('score' => $opt['ScoreCC'], 'color' => $opt['ColorCC']);
    }
    
    public function setCustomMarks($customMetrics)
    {  
        foreach ($customMetrics as $m) {
            $this->_customMarks[$m['Metric']]['A'] = $m['A'];
            $this->_customMarks[$m['Metric']]['B'] = $m['B'];
            $this->_customMarks[$m['Metric']]['C'] = $m['C'];
        }
        return $this;
    }
}